package com.jhd.jiangxi.vo;

import android.util.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;

import net.zigin.commons.util.CollectionUtil;
import net.zigin.commons.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 授权实体类
 *
 * @author mgw1986
 * @version V1.0
 * @date 2019/1/16 14:11
 */
public class AuthToken {
    @JSONField(name = "access_token")
    private String accessToken;
    @JSONField(name = "refresh_token")
    private String refreshToken;
    @JSONField(serialize = false)
    private long accessTokenExp;
    @JSONField(serialize = false)
    private long refreshTokenExp;
    @JSONField(serialize = false)
    private Map<String, Object> accessTokenMap = new HashMap<>();
    @JSONField(serialize = false)
    private Map<String, Object> refreshTokenMap = new HashMap<>();


    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public long getRefreshToken() {
        return refreshTokenExp;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public boolean isExpireAccessToken() {
        if (StringUtil.isNullOrEmpty(accessToken)) {
            return true;
        }
        if (CollectionUtil.isNullOrEmpty(accessTokenMap)) {
            accessTokenMap = parseToken(accessToken);
        }
        if (accessTokenExp == 0) {
            Object exp = accessTokenMap.get("exp");
            accessTokenExp = exp == null ? 0 : Long.parseLong(exp.toString()) * 1000L;
        }
        long currentTimeMillis = System.currentTimeMillis();
        return currentTimeMillis > accessTokenExp;
    }

    public long getAccessTokenExp() {
        return accessTokenExp;
    }

    public Object getAccessTokenByKey(String key) {
        if (CollectionUtil.isNullOrEmpty(accessTokenMap)) {
            accessTokenMap = parseToken(accessToken);
        }
        if (CollectionUtil.isNullOrEmpty(accessTokenMap) || !accessTokenMap.containsKey(key)) {
            return null;
        }
        return accessTokenMap.get(key);
    }

    public Object getRefreshTokenByKey(String key) {
        if (CollectionUtil.isNullOrEmpty(refreshTokenMap)) {
            refreshTokenMap = parseToken(refreshToken);
        }
        if (CollectionUtil.isNullOrEmpty(refreshTokenMap) || !refreshTokenMap.containsKey(key)) {
            return null;
        }
        return refreshTokenMap.get(key);
    }

    public boolean isExpireRefreshToken() {
        if (StringUtil.isNullOrEmpty(refreshToken)) {
            return true;
        }
        if (CollectionUtil.isNullOrEmpty(refreshTokenMap)) {
            refreshTokenMap = parseToken(refreshToken);
        }
        if (refreshTokenExp == 0) {
            Object exp = refreshTokenMap.get("exp");
            refreshTokenExp = exp == null ? 0 : Long.parseLong(exp.toString()) * 1000L;
        }
        long currentTimeMillis = System.currentTimeMillis();
        return currentTimeMillis > refreshTokenExp;
    }

    private Map<String, Object> parseToken(String token) {
        String[] payloadToken = token.split("\\.");
        if (payloadToken.length != 3) {
            return null;
        }
        String decodeString = new String(Base64.decode(payloadToken[1].getBytes(),Base64.URL_SAFE));
        //String decodeString = new String(Base64.decode(payloadToken[1].getBytes(), Base64.DEFAULT));//会报错java.lang.IllegalArgumentException: bad base-64
        return JSON.parseObject(decodeString, new TypeReference<Map<String, Object>>() {
        });
    }

}
